local xiancu = fk.CreateSkill{
  name = "xiaobai__xiancu"
} 

local xiancu_other = fk.CreateSkill{
  name = "xiaobai__xiancu&",
}

Fk:loadTranslationTable{
  ["xiaobai__xiancu"] = "羡簇",
  ["@xiaobai__xiancu-turn"] = "羡簇",
  ["#xiancu_chooseCard"] = "羡簇：是否将两张与本回合进入弃牌堆花色均不同的牌当【桃】对 %dest 使用？",
  ["xiaobai__xiancu&"] = "羡簇",
  [":xiaobai__xiancu"] = "一名角色于其出牌阶段或你濒死对其求【桃】时，可以将两张与本回合进入弃牌堆花色均不同的牌当【桃】对你使用。",
  [":xiaobai__xiancu&"] = "一名角色于其出牌阶段或或你濒死对其求【桃】时，可以将两张与本回合进入弃牌堆花色均不同的牌当【桃】对你使用。",
}

xiancu:addEffect("active",{
  attached_skill_name = "xiaobai__xiancu&",
  can_use = function (self, player)
    local c = Fk:cloneCard("peach")
    return player:isWounded() and not player:isProhibited(player, c)
  end,
  card_filter = function (self, player , to_select, selected)
    local suits = Self:getMark("@xiaobai__xiancu-turn")
    if type(suits) ~= "table" then
      suits = {}
    end
    if selected == nil or #selected == 0 then
      return not table.contains(suits,Fk:getCardById(to_select):getSuitString(true))
    end
    local c = Fk:cloneCard("peach")
    c:addSubcards(selected)
    c:addSubcard(to_select)
    return #selected < 2 and (not table.contains(suits,Fk:getCardById(to_select):getSuitString(true))) and Self:canUseTo(c, player)
  end,
  card_num = 2,
  on_use = function (self, room, effect)
    local player = effect.from
    local c = Fk:cloneCard("peach")
    c:addSubcards(effect.cards)
    room:useCard{
      from = player,
      tos = {player},
      card = c,
    }
  end
})
xiancu:addEffect(fk.AskForCardUse,{
  name = "#xiancu_trigger",
  mute = true,
  events = {fk.AskForCardUse},
  can_trigger = function (self, event, target, player, data)
    local card = Fk:cloneCard("peach")
    return  (target == player and player:hasSkill(self)) and data.extraData.must_targets and #data.extraData.must_targets>0 
      and player.room:getPlayerById(data.extraData.must_targets[1]):hasSkill(self) 
      and Exppattern:Parse(data.pattern):match(card)
  end,
  on_cost = function (self, event, target, player, data)
    local real,ret = player.room:askToUseActiveSkill(player,{
      skill_name = "xiancu_chooseCard",
      prompt = "#xiancu_chooseCard::"..data.extraData.must_targets[1],
      cancelable = true
    })
    if real and ret then  
      event:setCostData(self,ret.cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self)
    target = table.find(player.room:getAlivePlayers(),function (element, index, array)
      return element:hasSkill("xiaobai__xiancu")
    end)
    local c = Fk:cloneCard("peach")
    c:addSubcards(cards)
    c.skillName = "xiaobai__xiancu"
    data.result = {
      from = player.id,
      card = c
    }
    return true
  end,
})

xiancu:addEffect(fk.AfterCardsMove,{
  can_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if player.room:getCardArea(info.cardId) == Card.DiscardPile then
            return true
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and player.room:getCardArea(info.cardId) ~= Card.DiscardPile  then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("@xiaobai__xiancu-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if room:getCardArea(info.cardId) == Card.DiscardPile then
            table.insertIfNeed(ids, Fk:getCardById(info.cardId):getSuitString(true))
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and room:getCardArea(info.cardId) ~= Card.DiscardPile then
          table.removeOne(ids, Fk:getCardById(info.cardId):getSuitString(true))
        end
      end
    end
    room:setPlayerMark(player, "@xiaobai__xiancu-turn", ids)
  end
})

xiancu:addAcquireEffect(function (self, player, is_start)
  local ids = {}
  local room = player.room
  room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if room:getCardArea(info.cardId) == Card.DiscardPile then
            table.insertIfNeed(ids, Fk:getCardById(info.cardId):getSuitString(true))
          end
        end
      end
    end
  end, Player.HistoryTurn)
  room:setPlayerMark(player, "@xiaobai__xiancu-turn", ids)
end)

xiancu_other:addEffect("active",{
  can_use = function (self, player)
    local c = Fk:cloneCard("peach")
    local target = table.find(Fk:currentRoom().alive_players,function (element, index, array)
      return element:hasSkill('xiaobai__xiancu')
    end)
    return target and target:isWounded() and not player:isProhibited(player,c)
  end,
  card_filter = function (self, player, to_select, selected)
    local suits = player:getMark("@xiaobai__xiancu-turn")
    if type(suits) ~= "table" then
      suits = {}
    end
    if selected == nil or #selected == 0 then
      return not table.contains(suits,Fk:getCardById(to_select):getSuitString(true))
    end
    local c = Fk:cloneCard("peach")
    c:addSubcards(selected)
    c:addSubcard(to_select)
    return #selected < 2 and (not table.contains(suits,Fk:getCardById(to_select):getSuitString(true))) and not Self:prohibitUse(c)
  end,
  card_num = 2,
  on_use = function (self, room, effect)
    local target = table.find(room:getAlivePlayers(),function (element, index, array)
      return element:hasSkill("xiaobai__xiancu")
    end)
    local player = effect.from
    local c = Fk:cloneCard("peach")
    c:addSubcards(effect.cards)
    c.skillName = "xiaobai__xiancu"
    room:useCard{
      from = player,
      tos = {target},
      card = c,
    }
  end
})

xiancu_other:addEffect(fk.AskForCardUse,{
  name = "#xiancu_trigger",
  mute = true,
  events = {fk.AskForCardUse},
  can_trigger = function (self, event, target, player, data)
    local card = Fk:cloneCard("peach")
    return  (target == player and player:hasSkill(self)) and data.extraData.must_targets and #data.extraData.must_targets>0 
      and player.room:getPlayerById(data.extraData.must_targets[1]):hasSkill(self) 
      and Exppattern:Parse(data.pattern):match(card)
  end,
  on_cost = function (self, event, target, player, data)
    local real,ret = player.room:askToUseActiveSkill(player,{
      skill_name = "xiancu_chooseCard",
      prompt = "#xiancu_chooseCard::"..data.extraData.must_targets[1],
      cancelable = true
    })
    if real and ret then  
      event:setCostData(self,ret.cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self)
    target = table.find(player.room:getAlivePlayers(),function (element, index, array)
      return element:hasSkill("xiaobai__xiancu")
    end)
    local c = Fk:cloneCard("peach")
    c:addSubcards(cards)
    c.skillName = "xiaobai__xiancu"
    data.result = {
      from = player.id,
      card = c
    }
    return true
  end,
  refresh_events = {fk.AfterCardsMove, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if player.room:getCardArea(info.cardId) == Card.DiscardPile then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and player.room:getCardArea(info.cardId) ~= Card.DiscardPile  then
            return true
          end
        end
      end
    else
      return data == self and player.room:getTag("RoundCount")
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      local ids = player:getTableMark("@xiaobai__xiancu-turn")
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, Fk:getCardById(info.cardId):getSuitString(true))
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and room:getCardArea(info.cardId) ~= Card.DiscardPile then
            table.removeOne(ids, Fk:getCardById(info.cardId):getSuitString(true))
          end
        end
      end
      room:setPlayerMark(player, "@xiaobai__xiancu-turn", ids)
    else
      local ids = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if room:getCardArea(info.cardId) == Card.DiscardPile then
                table.insertIfNeed(ids, Fk:getCardById(info.cardId):getSuitString(true))
              end
            end
          end
        end
      end, Player.HistoryTurn)
      room:setPlayerMark(player, "@xiaobai__xiancu-turn", ids)
    end
  end,
})


local xiancu_chooseCard = fk.CreateSkill{
  name = "xiancu_chooseCard"
}

xiancu_chooseCard:addEffect("active",{
  mute = true,
  card_num = 2,
  card_filter = function (self, player, to_select, selected)
    local suits = Self:getMark("@xiaobai__xiancu-turn")
    if type(suits) ~= "table" then
      suits = {}
    end
    if selected == nil or #selected == 0 then
      return not table.contains(suits,Fk:getCardById(to_select):getSuitString(true))
    end
    local c = Fk:cloneCard("peach")
    c:addSubcards(selected)
    c:addSubcard(to_select)
    return #selected < 2 and (not table.contains(suits,Fk:getCardById(to_select):getSuitString(true))) and not Self:prohibitUse(c)
  end,
})

local skels = {}
table.insert(skels, xiancu)
table.insert(skels, xiancu_other)
table.insert(skels, xiancu_chooseCard)

return skels